<?php

namespace App\Services\Favorite\Favorite;

use App\Models\Favorite\FavoriteCategory;
use App\Services\Manage\ManageIndex;
use Illuminate\Support\Facades\DB;

/**
 * 收藏分类逻辑层
 * Class FavoriteCategoryLogic
 * @package App\Services\Favorite\Favorite
 */
class FavoriteCategoryLogic
{
    use \App\Services\Goods\Traits\UpdateCate;
    use \App\Services\User\Traits\addAppId;
    /*内部获取某一分类的详情*/
    public function dbCateInfo($data)
    {
        $_where['id'] = $data['id'];
        $res = FavoriteCategory::where($_where)->where('status','<>',-1)->first();
        return $res;
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            !$isManageAppId && $_where['app_id'] = $map['app_id'];
        }
       // $_where['status'] = ['neq',-1];
        $query = FavoriteCategory::where('status','<>',-1);
        if(!empty($_where)){
            $query->where($_where);
        }
        return $query->select();
    }
//    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
//    public function dbAllCateList($map = [])
//    {
//        isset($map['id']) && $_where['id'] = $map['id'];
//        isset($map['pid']) && $_where['pid'] = $map['pid'];
//        $_where['status'] = isset($map['status']) ? $map['status'] : ['neq',-1];
//
//        if(isset($map['app_id'])){   //若卖家查看商品详情，则不传入app_id
//            $manageAppId = \app\manage\service\ApiBase::getAppId();
//            $_whereOr = $manageAppId == $map['app_id'] ? true : '`app_id`=' . $map['app_id'] . ' OR FIND_IN_SET(' . $map['app_id'] . ',`other_app_read`) OR is_public = 1';
//        }else{
//            $_whereOr = true;
//        }
//        return $this->db()
//            ->where($_where)
//            ->where($_whereOr)
//            ->select();
//    }
//
    /*编辑收藏分类*/
    public function editCate($data): array
    {
        return $this->_updateCate('\App\Services\Favorite\Favorite\FavoriteCategoryLogic', '\App\Models\Favorite\FavoriteCategory', $data);
    }

    /*添加收藏分类*/
    public function addCate($data): array
    {
        try{
            $data['create_time'] = NOW;
            $data['update_time'] = NOW;
            $res = FavoriteCategory::insert($data);
        }catch(\Exception $e){
            return server_err(null, '数据库错误' . $e->getMessage() );
        }
       return auto_return($res, '添加分类成功','添加分类失败');
    }

//    /*获取某个分类的详情*/
//    public function oneCateInfo($data): array
//    {
//        isset($data['id']) && $_where['id'] = $data['id'];
//        isset($data['action']) && $_where['action'] = $data['action'];
//        $_where['status'] = ['neq',-1];
//        //$_where = $this->_addAppId($_where, $data['app_id']);
//        //app_id的筛选
//        if(isset($data['app_id'])){   //若卖家查看商品详情，则不传入app_id
//            $manageAppId = \app\manage\service\ApiBase::getAppId();
//            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
//        }else{
//            $_whereOr = true;
//        }
//        $res = $this->model($this->favoriteCategoryModel)->where($_where)->where($_whereOr)->find();
//        return auto_return($res);
//    }
//
//    /*获取树状分类列表*/
//    public function cateList($data): array
//    {
//        return $this->_getCateTree($data, $this->favoriteCategoryLogic, 'dbAllCateList');
//    }
//
//    /*删除一个或多个收藏分类*/
//    public function batchDelCate($data): array
//    {
//        is_numeric($data['id']) && $data['id'] = [$data['id']];
//        $map['pid'] = ['in',$data['id']];
//
//        $list = $this->dbCateList($map);
//        if(count($list) > 0){
//            return auto_failure(null,'失败，存在子类【' . $list[0]['name'] . '】');
//        }
//        $_where['id'] = ['in',$data['id']];
//        $_where = $this->_addAppId($_where, $data['app_id']);
//        $update['status'] = -1;
//        $res = $this->db()
//            ->where($_where)
//            ->update($update);
//        return auto_return(1,'删除成功','删除失败');
//    }
//
//    /*启用/禁用 一个/多个 分类  传参： id数组 */
//    public function batchChangeStatus($data): array
//    {
//        $list = $this->dbCateList();
//        $saveData['status'] = $data['status'];
//
//        is_numeric($data['id']) && $data['id'] = [$data['id']];
//        foreach ($data['id'] as $k => $v) {
//            $childId = child_ids($list, $v, 'id', 'pid', 'id');  //使用static数组 获取该分类下的所有子类
//        }
//        $data['id'] = array_merge($childId, $data['id']);
//        $_where['id'] = ['in',$data['id']];
//        $_where = $this->_addAppId($_where, $data['app_id']);
//
//        $res = $this->model($this->favoriteCategoryModel)->save($saveData, $_where);
//        return auto_return($res);
//    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array
    {
        $info = FavoriteCategory::where(['action'=>$data['action']])->first();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }



}